package com.app.service.impl;


import com.alibaba.fastjson.JSON;
import com.app.common.SystemConstant;
import com.app.conf.RedisService;
import com.app.service.LoginService;
import com.app.util.WXConstant;
import com.app.util.WxQrCode;
import com.app.vo.WxLoginVo;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.util.*;


/**
 * @Description:扫码登陆
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private static RedisService redisService;
    @Value("${web.upload-path}")
    private String filepath;

    @Override
    public WxLoginVo createAndGetQrcodeFile() throws Exception {
        // 1、创建文件名及scene值
        String scene = UUID.randomUUID().toString().substring(0,8);
        // 2、获取token
        String accessToken = WxQrCode.redisToken();
        System.out.println("accessToken = " + accessToken);
        // 3、请求图片流
        InputStream inputStream = getwxacode(accessToken, scene);
        System.out.println("input"+inputStream);
        // 4、保存图标文件
        saveToImgByInputStream(inputStream,scene);
        // 5、数据库增加该项数据
        WxLoginVo wx = new WxLoginVo();
        wx.setScene(scene);
        wx.setImgUrl(SystemConstant.DEFAULT_UPLOAD_SERVICE_URL+"wx-login/"+scene+".jpg");
        return wx;
    }


    /**
     1. 带参数有限个数小程序码接口
     2. @param url
     3. @param access_token
     4. @param path
     5. @param width
     6. @return
     */
    private static InputStream getwxacode(String accessToken, String scene){
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
        JSONObject jsonParam = new JSONObject();
        // 封装请求对象
        // scene值
        jsonParam.put("scene", scene);
        // 跳往的小程序页面，一般为认证界面
        jsonParam.put("page","page_other/login/scanning_result");
        // 检测页面是否存在，默认为 true
        jsonParam.put("check_path", false);
        // 图片宽度，默认为 430
        jsonParam.put("width","430");
        // 返回请求结果
        return doWxPost(url, jsonParam);
    }

    /**
     * IO流保存图片
     *
     * @param instreams 需要保存的流
     * @param imagePath 保存的图片路径
     * @param fileName  文件名
     * @return 保存状态
     */
    private static boolean uploadImages( InputStream instreams,String imagePath,String fileName) {
        File f = new File(imagePath);
        f.setWritable(true, false);
        boolean flag = false;
        try {
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流
            File file = new File(imagePath,fileName);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
                try {
                    // 创建新文件
                    file.createNewFile();
                } catch (IOException e) {
                    System.out.println("创建新文件时出现了错误。。。");
                    e.printStackTrace();
                }
            }
            OutputStream os = new FileOutputStream(imagePath+ File.separator+fileName);
            // 开始读取
            while ((len = instreams.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            // 完毕，关闭所有链接
            os.close();
            instreams.close();
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 通过流保存图片
     *
     * @param instreams 二进制流
     * @param fileName  图片的名称
     */
    private void saveToImgByInputStream(InputStream instreams, String fileName) {
        if (instreams != null) {
            boolean b = uploadImages(instreams, filepath, fileName + ".jpg");
        }
    }

    /**
     * 发送post请求
     *
     * @param url       请求地址
     * @param jsonParam 请求参数
     * @return 响应流
     */
    private static InputStream doWxPost(String url, JSONObject jsonParam) {
        InputStream instreams = null;
        // 创建HttpPost对象
        HttpPost httpRequst = new HttpPost(url);
        try {
            StringEntity se = new StringEntity(jsonParam.toString(),"utf-8");
            se.setContentType("application/json");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"UTF-8"));
            httpRequst.setEntity(se);
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    instreams = httpEntity.getContent();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return instreams;
    }
}
